package com.jfl.scheduler;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jfl.model.SysLog;
import com.jfl.monitor.SysLogService;
import com.jfl.utils.AddressUtils;

/**
 * 更新操作地址
 *  
 * @author  赵炎
 * @version  [V1.00, 2019年4月23日]
 * @see  [相关类/方法]
 * @since V1.00
 */
@Component
public class UpdateOperateLocationTask
{
    private static final Logger LOG = LoggerFactory.getLogger(UpdateOperateLocationTask.class);
    
    @Autowired
    private SysLogService sysLogService;
    
    @Scheduled(fixedDelay= 6000)
    public void run() throws InterruptedException
    {
        LOG.info("==> 【定时任务】 更新操作地址开始");
        QueryWrapper<SysLog> query = new QueryWrapper<SysLog> ();
//        query.eq("operate_location", "未知");
        query.isNull("operate_location");
        // 数据库字段
        List<SysLog> list = this.sysLogService.list(query);
        // 批量更新存在问题
        if (!CollectionUtils.isEmpty(list))
        {
            for (SysLog sysLog : list)
            {
//                if ("未知".equals(sysLog.getOperateLocation()) || StringUtils.isBlank(sysLog.getOperateLocation()))
//                {
                    //Thread.sleep(2000l);
                    sysLog.setOperateLocation(AddressUtils.getRealAddressByIP(sysLog.getOperateIp()));
                    this.sysLogService.update(sysLog);
//                }
            }
        }
        LOG.info("==> 【定时任务】 更新操作地址结束");
    }
}
